package com.example.controller.handler;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    // 全局异常拦截
    @ExceptionHandler
    public SaResult handlerException(Exception e) {
        if (e instanceof NotPermissionException || e instanceof NotRoleException) {
            return SaResult.error("权限不足");
        }
        if (e instanceof NotLoginException) {
            return SaResult.error("未登录");
        }
        e.printStackTrace();
        return SaResult.error(e.getMessage());
    }
}